import {useContext, useRef} from "react";
import {CanvasContext} from "@/views/dataScreen/Context";
import Canvas from "./canvas";

export const useCanvas = (canvas: any) => {
    const canvasRef: any = useRef();

    if (!canvasRef.current) {
        if (canvas) {
            canvasRef.current = canvas;
        } else {
            const canvas = new Canvas();
            canvasRef.current = canvas.getPublicCanvas();
        }
    }

    return canvasRef.current;
}

// 获取操作canvas数据的函数
export function useCanvasByContext() {
    const canvas = useContext(CanvasContext);

    return canvas;
}

// 获取的是画布数据，this.canvas
export function useCanvasData() {
    const canvas: any = useContext(CanvasContext);
    return canvas.getCanvas();
}

export function useCanvasCmps() {
    const canvas: any = useContext(CanvasContext);
    return canvas.getCanvasCmps();
}

